﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Threading;
using System.Xml;

namespace CountTools
{
    public partial class ResultFilterForm : System.Windows.Forms.Form
    {
        private Document _document;
        private Dictionary<string, List<string>> _elementList;
        private string _path;

        public ResultFilterForm()
        {
            InitializeComponent();
        }

        public ResultFilterForm(Document document)
        {
            InitializeComponent();
            _document = document;
            CalculateForm_Load();
        }
        private void CalculateForm_Load()
        {
            treeViewFloor.Nodes.Clear();
            TreeNode treeFloor = new TreeNode("楼层");
            treeViewFloor.Nodes.Add(treeFloor);
            IList<Element> elements = RevitCommon.ClassAndCategoryFilterElements<Level>(_document, BuiltInCategory.OST_Levels);
            foreach (Element element in elements)
            {
                Level level = element as Level;
                TreeNode treeNode = new TreeNode(level.Name);
                treeNode.Tag = level.Id;
                treeViewFloor.Nodes[0].Nodes.Add(treeNode);
            }
            treeViewFloor.Nodes[0].Expand();
            foreach (TreeNode node in treeViewFloor.Nodes)
            {
                node.Checked = true;
                TreeViewCheck.CheckAllChildNodes(node, true);
            }

            string path = _document.PathName;
            int lastChar = path.LastIndexOf(@"\");
            path = path.Remove(lastChar + 1);
            path += _document.Title.Substring(0, _document.Title.Length - 4) + "算量信息.xml";
            this._path = path;
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            List<string> elementTypes = new List<string>();
            Dictionary<string, List<string>> elementsList = new Dictionary<string, List<string>>();
            XmlNode mainNode = xml.SelectSingleNode("算量信息");
            var nodes = mainNode.ChildNodes;
            foreach (XmlNode childNode in nodes)
            {
                string elementId = childNode.Name;
                XmlNode name = childNode.SelectSingleNode("项目名称");
                XmlNode code = childNode.SelectSingleNode("项目编码");
                string codeText = code.InnerText;
                string nameText = name.InnerText;

                if (elementsList.ContainsKey(codeText))
                {
                    elementsList[codeText].Add(elementId);
                }
                else
                {
                    elementsList.Add(codeText, new List<string>());
                    elementsList[codeText].Add(elementId);
                    elementTypes.Add(nameText + " " + codeText);
                }
            }
            this._elementList = elementsList;
            elementTypes.Sort();

            //编码
            treeViewElement.Nodes.Clear();
            //TreeNode nodeA = new TreeNode("A土石方工程");
            //treeViewElement.Nodes.Add(nodeA);
            //TreeNode nodeB = new TreeNode("B地基处理与边坡支护工程");
            //treeViewElement.Nodes.Add(nodeB);
            //TreeNode nodeC = new TreeNode("C桩基工程");
            //treeViewElement.Nodes.Add(nodeC);
            //TreeNode nodeD = new TreeNode("D砌筑工程");
            //treeViewElement.Nodes.Add(nodeD);
            //TreeNode nodeE = new TreeNode("E混凝土及钢筋混凝土工程");
            //treeViewElement.Nodes.Add(nodeE);
            //TreeNode nodeF = new TreeNode("F金属结构工程");
            //treeViewElement.Nodes.Add(nodeF);
            //TreeNode nodeG = new TreeNode("G木结构工程");
            //treeViewElement.Nodes.Add(nodeG);
            //TreeNode nodeH = new TreeNode("H门窗工程");
            //treeViewElement.Nodes.Add(nodeH);
            //TreeNode nodeJ = new TreeNode("J屋面及防水工程");
            //treeViewElement.Nodes.Add(nodeJ);

            XmlDocument xmlRule = new XmlDocument();
            path = path.Replace("算量信息", "清单规则");
            xmlRule.Load(path);

            foreach (string elementType in elementTypes)
            {
                string temp = GetCountData(elementType.Split(' ')[1], path);
                string projectName = temp.Split('$')[0];
                string nodeClass = temp.Split('$')[1];
                string codeName = elementType.Split(' ')[1];
                string codeNum = elementType.Split(' ')[2];

                TreeNode node0 = null;
                foreach (TreeNode node in treeViewElement.Nodes)
                {
                    if (node.Text == projectName)
                    {
                        node0 = node;
                        break;
                    }
                }
                if (node0 == null)
                {
                    node0 = new TreeNode(projectName);
                    treeViewElement.Nodes.Add(node0);
                }

                TreeNode node1 = null;
                foreach (TreeNode node in node0.Nodes)
                {
                    if (node.Text == nodeClass)
                    {
                        node1 = node;
                        break;
                    }
                }
                if (node1 == null)
                {
                    node1 = new TreeNode(nodeClass);
                    node0.Nodes.Add(node1);
                }

                TreeNode node2 = null;
                foreach (TreeNode node in node1.Nodes)
                {
                    if (node.Text == codeName)
                    {
                        node2 = node;
                        break;
                    }
                }
                if (node2 == null)
                {
                    node2 = new TreeNode(codeName);
                    node1.Nodes.Add(node2);
                }

                TreeNode node3 = null;
                foreach (TreeNode node in node2.Nodes)
                {
                    if (node.Text == codeNum)
                    {
                        node3 = node;
                        break;
                    }
                }
                if (node3 == null)
                {
                    node3 = new TreeNode(codeNum);
                    node2.Nodes.Add(node3);
                }

                node0.ExpandAll();
                node1.ExpandAll();
                node2.ExpandAll();
            }



            foreach (TreeNode node in treeViewElement.Nodes)
            {
                node.Checked = true;
                TreeViewCheck.CheckAllChildNodes(node, true);
            }
        }

        private string GetCountData(string temp, string path)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNode childNode = xml.SelectSingleNode("工程量计算规范");

            foreach (XmlNode xmlNode in childNode.ChildNodes)
            {
                foreach (XmlNode xmlElement in xmlNode.ChildNodes)
                {
                    foreach (XmlNode childElem in xmlElement.ChildNodes)
                    {
                        if (childElem.Name == temp)
                        {
                            return xmlNode.Name + "$" + xmlElement.Name;
                        }
                    }
                }
            }
            return "";
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            List<string> level = new List<string>();
            foreach (TreeNode node in treeViewFloor.Nodes[0].Nodes)
            {
                if (node.Checked)
                {
                    level.Add(node.Text);
                }
            }

            Dictionary<string, List<string>> resultsList = new Dictionary<string, List<string>>();
            foreach (TreeNode node1 in treeViewElement.Nodes)
            {
                foreach (TreeNode node2 in node1.Nodes)
                {
                    foreach (TreeNode node3 in node2.Nodes)
                    {
                        foreach (TreeNode node4 in node3.Nodes)
                        {
                            if (node4.Checked)
                            {
                                string codeNum = node4.Text;
                                resultsList.Add(codeNum, new List<string>());
                                resultsList[codeNum].AddRange(_elementList[codeNum]);
                            }
                        }
                    }
                }
            }






            this.Hide();
            ReportForm reportForm = new ReportForm(level, resultsList, _path);
            reportForm.ShowDialog();

        }


        private void treeViewElement_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeViewCheck.CheckControl(e);
        }

        private void treeViewFloor_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeViewCheck.CheckControl(e);
        }

        private void buttonFloorCheckAll_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeViewFloor.Nodes)
            {
                node.Checked = true;
                TreeViewCheck.CheckAllChildNodes(node, true);
            }
        }

        private void buttonFloorCheckNone_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeViewFloor.Nodes)
            {
                node.Checked = false;
                TreeViewCheck.CheckAllChildNodes(node, false);
            }
        }

        private void buttonFloorReverseCheck_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeViewFloor.Nodes)
            {
                TreeViewCheck.CheckAllChildNodes(node);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
